<?php
class Campaigns_work
{
	const DBTable = "tbl_campaigns_work";

	public $Work_id;	public $Work_md5;	public $Campaign_id;	public $Title;	public $Description;
	public $Description_panel1_title;
	public $Description_panel2_title;
	public $Description_brief;
	public $Description_solution;	public $Image;
	public $Image_vimeo_replacement;	public $Vimeo_url;
	public $Ordering;
	public $Ordering_latest;
	public $Date;
	public $Show_social;
	public $Is_classic;
	public $Show_homepage;
	public $Is_campaign_profile;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	public $Url_slug;
	public $Meta_title;
	public $Meta_description;
	public $Meta_keywords;
	public $Social_description;
	public $Bitly_url;
	private $__mysql;

	public function __construct()
	{
		$this->Work_id = 0;		$this->Work_md5 = null;		$this->Campaign_id = 0;		$this->Title = null;		$this->Description = null;
		$this->Description_panel1_title = null;
		$this->Description_panel2_title = null;
		$this->Description_brief = null;
		$this->Description_solution = null;		$this->Image = null;
		$this->Image_vimeo_replacement = null;		$this->Vimeo_url = null;
		$this->Ordering = 0;
		$this->Ordering_latest = 0;
		$this->Date = date("Y-m-d");
		$this->Show_social = 0;
		$this->Is_classic = 0;
		$this->Show_homepage = 0;
		$this->Is_campaign_profile = 0;		$this->Status = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->Url_slug = null;
		$this->Meta_title = null;
		$this->Meta_description = null;
		$this->Meta_keywords = null;
		$this->Social_description = null;
		$this->Bitly_url = null;
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
			(campaign_id, title, meta_title, description, description_panel1_title, description_panel2_title, description_brief, description_solution, image_thumbnail, image, image_vimeo_replacement, vimeo_url, show_social, status, createdate, updatedate, url_slug, is_classic, show_homepage, date)
			values
			(
			'".$this->Campaign_id."', 			'".mysql_real_escape_string($this->Title)."', 
			'".mysql_real_escape_string($this->Title)."', 			'".mysql_real_escape_string($this->Description)."', 
			'".mysql_real_escape_string($this->Description_panel1_title)."', 
			'".mysql_real_escape_string($this->Description_panel2_title)."', 
			'".mysql_real_escape_string($this->Description_brief)."', 
			'".mysql_real_escape_string($this->Description_solution)."', 			'".mysql_real_escape_string($this->Image_thumbnail)."', 
			'".mysql_real_escape_string($this->Image)."', 
			'".mysql_real_escape_string($this->Image_vimeo_replacement)."', 
			'".mysql_real_escape_string($this->Vimeo_url)."', 
			'".mysql_real_escape_string($this->Show_social)."', 			'".$this->Status."', 			'".$this->Createdate."', 			'".$this->Updatedate."',
			'".clean_url_accents(mysql_real_escape_string($this->Title))."',
			'".mysql_real_escape_string($this->Is_classic)."', 
			'".mysql_real_escape_string($this->Show_homepage)."',
			'".format_db_insert_date($this->Date)."'
			)";			
			//echo $sqlStr."<br />";
			$this->__mysql->Exec_query($sqlStr);
			$this->Work_id = $this->__mysql->Get_insert_id();
			
			//create an md5
			$this->Work_md5 = substr(md5($this->Work_id), 0, 10);
			$this->Quick_update("work_md5", $this->Work_md5);

			$this->Optimize_table();
			
			return $this->Work_id;
	}

	public function Update()
	{
		//deal with any assets
		$query = "select image, image_thumbnail, image_vimeo_replacement from ".self::DBTable." where work_id = '".mysql_real_escape_string($this->Work_id)."'";
		$recordset = $this->__mysql->Exec_query($query);
		$result = $this->__mysql->Get_data_assoc($recordset);

		if($this->Image != "" || $this->Image == "delete")
		{
			if($this->Image == "delete")
			{
				$this->Image = null;
				@unlink(ASSET_STORE_CAMPAIGNS_WORK . $result['image']);
			}

			$this->Quick_update("image", $this->Image);
		}

		if($this->Image_thumbnail != "" || $this->Image_thumbnail == "delete")
		{
			if($this->Image_thumbnail == "delete")
			{
				$this->Image_thumbnail = null;
				@unlink(ASSET_STORE_CAMPAIGNS_WORK . $result['image_thumbnail']);
			}

			$this->Quick_update("image_thumbnail", $this->Image_thumbnail);
		}

		if($this->Image_vimeo_replacement != "" || $this->Image_vimeo_replacement == "delete")
		{
			if($this->Image_vimeo_replacement == "delete")
			{
				$this->Image_vimeo_replacement = null;
				@unlink(ASSET_STORE_CAMPAIGNS_WORK . $result['image_vimeo_replacement']);
			}

			$this->Quick_update("image_vimeo_replacement", $this->Image_vimeo_replacement);
		}
		//
	
		$sqlStr = "update ".self::DBTable." set
			campaign_id = '".$this->Campaign_id."', 			title = '".mysql_real_escape_string($this->Title)."', 			description = '".mysql_real_escape_string($this->Description)."', 
			description_panel1_title = '".mysql_real_escape_string($this->Description_panel1_title)."', 
			Description_panel2_title = '".mysql_real_escape_string($this->Description_panel2_title)."', 
			description_brief = '".mysql_real_escape_string($this->Description_brief)."', 
			description_solution = '".mysql_real_escape_string($this->Description_solution)."', 
			image_thumbnail = '".mysql_real_escape_string($this->Image_thumbnail)."', 			image_thumbnail = '".mysql_real_escape_string($this->Image_thumbnail)."', 
			image_vimeo_replacement = '".mysql_real_escape_string($this->Image_vimeo_replacement)."',
			image = '".mysql_real_escape_string($this->Image)."', 			vimeo_url = '".mysql_real_escape_string($this->Vimeo_url)."', 
			show_social = '".mysql_real_escape_string($this->Show_social)."', 
			is_classic = '".mysql_real_escape_string($this->Is_classic)."',
			show_homepage = '".mysql_real_escape_string($this->Show_homepage)."', 			status = '".$this->Status."', 
			updatedate = '".$this->Updatedate."', 
			url_slug = '".clean_url_accents(mysql_real_escape_string($this->Url_slug))."',
			date = '".format_db_insert_date($this->Date)."'
			where work_id = '".mysql_real_escape_string($this->Work_id)."'";
			$this->__mysql->Exec_query($sqlStr);
		//echo $sqlStr;
		
		//right I need to also update the updated datetime value against this campaign so that when the user is on the clients page in the frontend its ordered by when the work item was last updated
		$sqlStr = "update tbl_campaigns set updatedate = '".$this->Updatedate."' where campaign_id = ".$this->Campaign_id;
		$this->__mysql->Exec_query($sqlStr);
		
		//exit;
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where work_id = '".mysql_real_escape_string($this->Work_id)."'";
			$this->__mysql->Exec_query($sqlStr);
		//echo $sqlStr . "<br />";
	}

	public function Update_ordering($id_array = null, $campaign_id = 0)
	{
		if(count($id_array) > 0)
		{
			$i = 1;
			foreach($id_array as $id)
			{
				//update the ordering of the banner that is about to be replaced
				$sqlStr = "update ".self::DBTable." 
					set ordering = ".$i." 
					where 
					campaign_id = '".$campaign_id."' and 
					work_id = '".$id."'";
				//echo $sqlStr . "<br />";
				$this->__mysql->Exec_query($sqlStr);
				$i++;
			}
		}
		$this->Optimize_table();
	}

	//this is the global ordering for all work items which is used on the latest page
	public function Update_ordering_latest($id_array = null)
	{
		if(count($id_array) > 0)
		{
			$i = 1;
			foreach($id_array as $id)
			{
				//update the ordering of the banner that is about to be replaced
				$sqlStr = "update ".self::DBTable." 
					set ordering_latest = ".$i." 
					where 
					work_id = '".$id."'";
				//echo $sqlStr . "<br />";
				$this->__mysql->Exec_query($sqlStr);
				$i++;
			}
		}
		$this->Optimize_table();
	}

	public function Update_campaign_profile($work_id = 0)
	{
		//load in the campaign_id from the work_id
		if($this->Select($work_id))
		{		
			//remove the current profile set
			$sqlStr = "update ".self::DBTable." set is_campaign_profile = 0 where campaign_id = ".$this->Campaign_id;
			$this->__mysql->Exec_query($sqlStr);

			$sqlStr = "update ".self::DBTable." set is_campaign_profile = 1 where work_id = ".$work_id;
			$this->__mysql->Exec_query($sqlStr);
		}
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Get_global_work_items($limit = null, $offset = null)
	{
		$sqlStr = "select cpw.*, cp.title as campaign_title, c.title as client_title
			from ".self::DBTable." cpw
			inner join tbl_campaigns cp on cpw.campaign_id = cp.campaign_id
			inner join tbl_clients c on cp.client_id = c.client_id
			where cpw.is_deleted = 0 
			order by cpw.date desc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_by_campaign_id($campaign_id = 0, $limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where 
			is_deleted = 0 and 
			campaign_id = ".$campaign_id."
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Homepage_campaign_work($limit = null, $offset = null)
	{
		$sqlStr = "select cpw.work_id, cpw.title, cp.title as campaign_title, c.title as client_title
			from ".self::DBTable." cpw
			inner join tbl_campaigns cp on cp.campaign_id = cpw.campaign_id
			inner join tbl_clients c on cp.client_id = c.client_id
			where cpw.is_deleted = 0 and 
			cpw.show_homepage = 1 and
			cp.status = 1 
			order by client_title, campaign_title, cpw.title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array_assoc($recordset);
	}

	public function List_all_by_campaign_id_frontend($campaign_id = 0, $limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where 
			is_deleted = 0 and 
			status = 1 and 
			campaign_id = ".$campaign_id."
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select work_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($work_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where work_id = '".mysql_real_escape_string($work_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$this->Optimize_table();
	}

	public function Optimize_table()
	{
		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}


	public function Select($work_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where work_id = '".mysql_real_escape_string($work_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Work_id = $result['work_id'];				$this->Work_md5 = $result['work_md5'];				$this->Campaign_id = $result['campaign_id'];				$this->Title = $result['title'];				$this->Description = $result['description'];
				$this->Description_panel1_title = $result['description_panel1_title'];
				$this->Description_panel2_title = $result['description_panel2_title'];
				$this->Description_brief = $result['description_brief'];
				$this->Description_solution = $result['description_solution'];
				$this->Image_thumbnail = $result['image_thumbnail'];				$this->Image = $result['image'];
				$this->Image_vimeo_replacement = $result['image_vimeo_replacement'];				$this->Vimeo_url = $result['vimeo_url'];				$this->Ordering = $result['ordering'];
				$this->Ordering_latest = $result['ordering_latest'];
				$this->Date = $result['date'];
				$this->Is_classic = $result['is_classic'];
				$this->Show_homepage = $result['show_homepage'];
				$this->Show_social = $result['show_social'];
				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				$this->Url_slug = $result['url_slug'];
				$this->Meta_title = $result['meta_title'];
				$this->Meta_description = $result['meta_description'];
				$this->Meta_keywords = $result['meta_keywords'];
				$this->Social_description = $result['social_description'];
				$this->Bitly_url = $result['bitly_url'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Work_id = $result['work_id'];
				$this->Work_md5 = $result['work_md5'];
				$this->Campaign_id = $result['campaign_id'];
				$this->Title = $result['title'];
				$this->Description = $result['description'];
				$this->Description_panel1_title = $result['description_panel1_title'];
				$this->Description_panel2_title = $result['description_panel2_title'];
				$this->Description_brief = $result['description_brief'];
				$this->Description_solution = $result['description_solution'];
				$this->Image_thumbnail = $result['image_thumbnail'];
				$this->Image = $result['image'];
				$this->Image_vimeo_replacement = $result['image_vimeo_replacement'];
				$this->Vimeo_url = $result['vimeo_url'];
				$this->Ordering = $result['ordering'];
				$this->Ordering_latest = $result['ordering_latest'];
				$this->Date = $result['date'];
				$this->Is_classic = $result['is_classic'];
				$this->Show_homepage = $result['show_homepage'];
				$this->Show_social = $result['show_social'];
				$this->Status = $result['status'];
				$this->Is_deleted = $result['is_deleted'];
				$this->Createdate = $result['createdate'];
				$this->Updatedate = $result['updatedate'];
				$this->Url_slug = $result['url_slug'];
				$this->Meta_title = $result['meta_title'];
				$this->Meta_description = $result['meta_description'];
				$this->Meta_keywords = $result['meta_keywords'];
				$this->Social_description = $result['social_description'];
				$this->Bitly_url = $result['bitly_url'];

				return true;
			}
			else
			{
				return false;
			}
		}
	}

	public function Select_by_campaign_id_work_slug($campaign_id = 0, $work_slug = null)
	{
		$sqlStr = "select * from ".self::DBTable." where url_slug = '".mysql_real_escape_string($work_slug)."' and campaign_id = ".$campaign_id."";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
			$this->Work_id = $result['work_id'];
			$this->Work_md5 = $result['work_md5'];
			$this->Campaign_id = $result['campaign_id'];
			$this->Title = $result['title'];
			$this->Description = $result['description'];
			$this->Description_panel1_title = $result['description_panel1_title'];
			$this->Description_panel2_title = $result['description_panel2_title'];
			$this->Description_brief = $result['description_brief'];
			$this->Description_solution = $result['description_solution'];
			$this->Image_thumbnail = $result['image_thumbnail'];
			$this->Image = $result['image'];
			$this->Image_vimeo_replacement = $result['image_vimeo_replacement'];
			$this->Vimeo_url = $result['vimeo_url'];
			$this->Ordering = $result['ordering'];
			$this->Ordering_latest = $result['ordering_latest'];
			$this->Date = $result['date'];
			$this->Is_classic = $result['is_classic'];
			$this->Show_homepage = $result['show_homepage'];
			$this->Show_social = $result['show_social'];
			$this->Status = $result['status'];
			$this->Is_deleted = $result['is_deleted'];
			$this->Createdate = $result['createdate'];
			$this->Updatedate = $result['updatedate'];
			$this->Url_slug = $result['url_slug'];
			$this->Meta_title = $result['meta_title'];
			$this->Meta_description = $result['meta_description'];
			$this->Meta_keywords = $result['meta_keywords'];
			$this->Social_description = $result['social_description'];
			$this->Bitly_url = $result['bitly_url'];

			return true;
		}
		else
		{
			return false;
		}
	}
}
?>